package com.intexh.quickandroid.sharesdk;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Looper;
import android.os.Message;
import android.widget.Toast;

import com.mob.MobSDK;

import java.io.Serializable;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import com.intexh.quickandroid.utils.GsonUtils;

public class ShareSdkLoginApi implements Callback {
	private static final int MSG_AUTH_CANCEL = 1;
	private static final int MSG_AUTH_ERROR= 2;
	private static final int MSG_AUTH_COMPLETE = 3;
	private final ShareLoginListener loginListener;

	private String platform;
	private Context context;
	private Handler handler;

	public ShareSdkLoginApi(ShareLoginListener loginListener) {
		handler = new Handler(Looper.getMainLooper(), this);
		this.loginListener = loginListener;
	}

	public void setPlatform(String platform) {
		this.platform = platform;
	}


	public void login(Activity context) {
		this.context = context;
		//初始化SDK
		MobSDK.init(context);
		Platform plat = ShareSDK.getPlatform(platform);
		if (plat == null) {
			loginListener.onShareLoginCallBack(AUTH_INIT_FAIL,platform,null);
			return;
		}
		loginListener.onShareLoginCallBack(AUTH_START,platform,null);
		if (plat.isAuthValid()) {
//			loginListener.onShareLoginCallBack(AUTH_LAST_SUCCESS,platform,null);
			plat.removeAccount(true);
//			return;
		}
		//使用SSO授权，通过客户单授权
		plat.SSOSetting(false);
		plat.setPlatformActionListener(new PlatformActionListener() {
			public void onComplete(Platform plat, int action, HashMap<String, Object> res) {
				if (action == Platform.ACTION_USER_INFOR) {
					Message msg = new Message();
					msg.what = MSG_AUTH_COMPLETE;
					msg.arg2 = action;
					msg.obj =  plat.getName();
					handler.sendMessage(msg);
				}
			}

			public void onError(Platform plat, int action, Throwable t) {
				if (action == Platform.ACTION_USER_INFOR) {
					Message msg = new Message();
					msg.what = MSG_AUTH_ERROR;
					msg.arg2 = action;
					msg.obj = t;
					handler.sendMessage(msg);
				}
				t.printStackTrace();
			}

			public void onCancel(Platform plat, int action) {
				if (action == Platform.ACTION_USER_INFOR) {
					Message msg = new Message();
					msg.what = MSG_AUTH_CANCEL;
					msg.arg2 = action;
					msg.obj = plat;
					handler.sendMessage(msg);
				}
			}
		});
//        plat.authorize();
		plat.showUser(null);
	}

	/**处理操作结果*/
	public boolean handleMessage(Message msg) {
		switch(msg.what) {
			case MSG_AUTH_CANCEL: {
				loginListener.onShareLoginCallBack(AUTH_CANCEL,platform,null);
			} break;
			case MSG_AUTH_ERROR: {
				loginListener.onShareLoginCallBack(AUTH_FAIL,platform,null);
				// 失败
				Throwable t = (Throwable) msg.obj;
				String text = "第三方登录异常: " + t.getMessage();
				Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
				t.printStackTrace();
			} break;
			case MSG_AUTH_COMPLETE: {
				// 成功
				final String platformName = (String) msg.obj;
				Platform platform = ShareSDK.getPlatform(platformName);
				PlatformDb db = platform.getDb();
				String userName = db.getUserName();
				String avatar = db.getUserIcon();
				String userGender = db.getUserGender(); // 性别，m：男、f：女、n：未知
				String userId = db.getUserId();
//				String openid = GsonUtils.getStringFromJSON(db.exportData(),"openid");
				String unionid = GsonUtils.getStringFromJSON(db.exportData(),"unionid");
				ShareLoginBean shareLoginBean = new ShareLoginBean();
				shareLoginBean.setOpenid(userId);//userid就是openid
				shareLoginBean.setUnionid(unionid); //qqunionid为null
				shareLoginBean.setNickname(userName);
				shareLoginBean.setAvatar(avatar);
				shareLoginBean.setSex(userGender);
				loginListener.onShareLoginCallBack(AUTH_SUCCESS,platformName,shareLoginBean);
			} break;
		}
		return false;
	}



	public static final int AUTH_SUCCESS = 0x1fe;
	public static final int AUTH_FAIL = 0x2fe;
	public static final int AUTH_CANCEL = 0x3fe;
	public static final int AUTH_INIT_FAIL = 0x4fe;
	public static final int AUTH_START = 0x5fe;
	public interface ShareLoginListener{
		void onShareLoginCallBack(int type, String loginType, ShareLoginBean bean);
	}

	public class ShareLoginBean implements Serializable{
		private String unionid;
		private String openid;
		private String nickname;
		private String avatar;
		private String sex;
		private String province;
		private String city;

		public String getUnionid() {
			return unionid;
		}

		public void setUnionid(String unionid) {
			this.unionid = unionid;
		}

		public String getOpenid() {
			return openid;
		}

		public void setOpenid(String openid) {
			this.openid = openid;
		}

		public String getNickname() {
			return nickname;
		}

		public void setNickname(String nickname) {
			this.nickname = nickname;
		}

		public String getAvatar() {
			return avatar;
		}

		public void setAvatar(String avatar) {
			this.avatar = avatar;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}

		public String getProvince() {
			return province;
		}

		public void setProvince(String province) {
			this.province = province;
		}

		public String getCity() {
			return city;
		}

		public void setCity(String city) {
			this.city = city;
		}
	}



}
